using UnityEngine;

public class MoveStateMachine : StateMachine
{
    public class MoveState : IState
    {
        public MoveStateMachine _stateMachine;
        public GameObject _controllerCarrier;
        public MainFrame _cruiser;

        public Vector2 _inputDirection;
        public Vector2 _currentDirection;

        public MoveState(MoveStateMachine stateMachine, GameObject controllerCarrier)
        {
            _stateMachine = stateMachine;
            _controllerCarrier = controllerCarrier;
            _cruiser = controllerCarrier.GetComponent<MainFrame>();
        }

        public virtual void IEnter() { }
        public virtual void IExit() { }
        public virtual void ILogicUpdate()
        {
            _inputDirection.x = Input.GetAxisRaw("Horizontal");
            _inputDirection.y = Input.GetAxisRaw("Vertical");
        }
        public virtual void IPhysicUpdate() { }
    }
    public class MoveState_Idle : MoveState
    {
        public MoveState_Idle(MoveStateMachine stateMachine, GameObject controllerCarrier) : base(stateMachine, controllerCarrier) { }

        public override void IEnter()
        {
            _stateMachine._fire.SetActive(false);
        }

        public override void ILogicUpdate()
        {
            base.ILogicUpdate();
            _cruiser.ON_Idle();
            if (_inputDirection.x != 0 || _inputDirection.y != 0)
            {
                _stateMachine.SwitchState(_stateMachine._moveState_Moving);
                return;
            }
        }
    }
    public class MoveState_Moving : MoveState
    {
        private GameObject _fire;
        private SpriteRenderer _fireSpriteRenderer;
        private Rigidbody2D _rigidbody2D;

        private int _color_H;

        public MoveState_Moving(MoveStateMachine stateMachine, GameObject fire, GameObject controllerCarrier) : base(stateMachine, controllerCarrier)
        {
            _fire = fire;
            _fireSpriteRenderer = _fire.GetComponent<SpriteRenderer>();
            _rigidbody2D = controllerCarrier.GetComponent<Rigidbody2D>();
        }

        public override void IEnter()
        {
            _fire.SetActive(true);
            _color_H = 0;
        }

        public override void ILogicUpdate()
        {
            base.ILogicUpdate();
            if (_inputDirection.x == 0 && _inputDirection.y == 0)
            {
                _stateMachine.SwitchState(_stateMachine._moveState_Idle);
                return;
            }
            _cruiser.ON_Move();
            _controllerCarrier.transform.rotation = Quaternion.RotateTowards(_controllerCarrier.transform.rotation, Quaternion.Euler(0, 0, Vector2.SignedAngle(Vector2.up, _inputDirection)), _cruiser._frameProperties.TurningSpeed / ConstValue.Scale);
            _rigidbody2D.velocity = _controllerCarrier.transform.up * _cruiser._frameProperties.MoveSpeed / ConstValue.Scale;
            _fireSpriteRenderer.color = Color.HSVToRGB(0.5f + 0.5f * Mathf.Sin(_color_H++ * 0.05f), /*Random.Range(0.6f, 1)*/1, 1);
        }
    }

    public MoveState_Idle _moveState_Idle;
    public MoveState_Moving _moveState_Moving;
    public GameObject _fire;

    private void Awake()
    {
        _moveState_Idle = new MoveState_Idle(this, gameObject);
        _moveState_Moving = new MoveState_Moving(this, _fire, gameObject);
        SwitchOn(_moveState_Idle);
    }
}
